package edu.flying.panda.taskmanager;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Login Screen
 * @author FlyingPanda
 */

public class LogInScreen extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log_in);
	    
		final Button logIn_button = (Button) findViewById(R.id.log_in_logIn_button);
		final Button signUp_button = (Button) findViewById(R.id.log_in_register_button);

		
		/**
		 * Listener for LogIn button, checks for correct Username/Password and handles errors
		 */
		logIn_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				String username = ((EditText) findViewById(R.id.log_in_username)).getText().toString();
				String password = ((EditText) findViewById(R.id.log_in_pass)).getText().toString();
				TextView errorText = (TextView) findViewById(R.id.log_in_error);
		
				//login check
				User user = TaskManagerActivity.DB.getUser(username);
				
				if (user!=null){
					if (user.getPassword().equals(password)){  ///tell dnt ask
							
						TaskManagerActivity.DB.setCURRENT_USERID(user.getId());
						Intent myIntent = new Intent(v.getContext(),HomeScreen.class);
						startActivity(myIntent);
							
					}else {
						errorText.setText("Username/Password incorrect");
					}
				} else {
					errorText.setText("Username/Password incorrect");
				}
			}
		});
		
		/**
		 * Listener for SignUp button, takes the User to the SignUp screen
		 */
		signUp_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(), SignUpScreen.class);
				startActivityForResult(myIntent, 1);
			}

		});

	}

}
